﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.SalesOrder;
using WcfService1.DTO;

namespace MSIACCSL2.Views.InventoryAdjustment
{
    public partial class FrmAdd : Page
    {
        private SearchItem si;
        private TextBox txtCurrentItem;
        private InventoryAdjustmentDetailDTO adjDetail;
        public InventoryAdjustmentWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as InventoryAdjustmentWrapViewModel;
            }
        }

        public InventoryAdjustmentViewModel ViewModel
        {
            get
            {
                return this.DataContext as InventoryAdjustmentViewModel;
            }
        }

        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as InventoryAdjustmentWrapViewModel).InventoryAdjustmentViewModel =
                                        (InventoryAdjustmentViewModel)this.DataContext;
                               };

            this.KeyDown += (o, e) =>
            {
                if (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.AddInventoryAdjustmentDetail(null);
                else if (e.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.DeleteInventoryAdjustmentDetail(null);
                else if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.SaveInventoryAdjustment(ViewModel.InventoryAdjustment);
                else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.Cancel(null);
            };
           
        }


        private void txtNoLog_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	if (e.Key == Key.Enter)
                WrapViewModel.GetInventory((sender as TextBox).DataContext);
        }

       


        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
           (sender as TextBox).SelectAll();
        }

        private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtCurrentItem = sender as TextBox;
                e.Handled = true;
                adjDetail = ((TextBox)sender).DataContext as InventoryAdjustmentDetailDTO;
                ViewModel.GetItemByName(((TextBox)sender).DataContext);
                si = new SearchItem();
                si.Closed += new EventHandler(si_Closed);
                si.DataContext = ViewModel;
                si.Show();
            }
        }

        void si_Closed(object sender, EventArgs e)
        {
            if (si.dg1.SelectedItem != null)
            {
                var item = si.dg1.SelectedItem as ItemDTO;
                adjDetail.Inventory.SatuanPack = new SatuanDTO(){KodeSatuan = item.SatuanPack.KodeSatuan};
                adjDetail.Inventory.SatuanUnit = item.Satuan;
            }
            DataGrid1.Focus();
            txtCurrentItem.Focus();

        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
     

        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            InventorySearch inventorySearch = new InventorySearch();
            inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
            inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            adjDetail = DataGrid1.SelectedItem as InventoryAdjustmentDetailDTO;
            adjDetail.Inventory = inv;
            if (adjDetail.Inventory != null)
            {
                adjDetail.Inventory.SatuanPack =
                    WrapViewModel.SatuanPacks.FirstOrDefault(x => x.KodeSatuan == inv.SatuanPack.KodeSatuan);
                adjDetail.Inventory.SatuanUnit =
                    WrapViewModel.SatuanUnits.FirstOrDefault(x => x.KodeSatuan == inv.SatuanUnit.KodeSatuan);
                adjDetail.Panjang = inv.Panjang;
                adjDetail.Lebar = inv.Lebar;
                adjDetail.Tinggi = inv.Warehouses.FirstOrDefault().Sheet;
                adjDetail.QtyUnit = inv.Warehouses.FirstOrDefault().Qty;
            }
            DataGrid1.Focus();
        }

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel.CloseStatus();
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //InventoryAdjustmentDetailDTO adjustmentDetail = (sender as TextBox).DataContext as InventoryAdjustmentDetailDTO;
            //if (adjustmentDetail != null)
            //    if (adjustmentDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", adjustmentDetail.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", adjustmentDetail.QtyUnit);
            //    }
        }
     
    }
}

